﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using WebStudio.Business.Entity;
using WebStudio.Framework.Helper;

namespace WebStudio.Business.Rule
{
    public class FriendUrlRule
    {
        #region 公有方法
        /// <summary>
        /// 获取信息列表
        /// </summary>
        /// <returns></returns>
        public static List<FriendUrlEntity> GetFriendUrlList()
        {
            List<FriendUrlEntity> list = new List<FriendUrlEntity>();
            using (var dr = DBHelper.ExecuteReader(string.Format("SELECT * FROM FriendUrlTB")))
            {
                return FillDate(dr);
            }
        }

        public static List<FriendUrlEntity> GetFriendUrlList(int pageIndex, int pageSize, int pageCount, int recordCount)
        {
            List<FriendUrlEntity> list = new List<FriendUrlEntity>();
            string strSql = string.Empty;
            if (pageIndex >= pageCount)
            {
                if (recordCount == 0)
                {
                    strSql = "select * from (select top " +  pageSize + " * from (select top " + pageIndex * pageSize + " * FROM FriendUrlTB order by FriendUrlId desc) order by FriendUrlId) order by FriendUrlId desc";
                }
                else
                {
                    strSql = "select * from (select top " + (recordCount - (pageIndex - 1) * pageSize) + " * from (select top " + pageIndex * pageSize + " * FROM FriendUrlTB order by FriendUrlId desc) order by FriendUrlId) order by FriendUrlId desc";
                }
            }
            else
            {
                strSql = "select * from (select top " + pageSize + " * from (select top " + pageIndex * pageSize + "  * FROM FriendUrlTB order by FriendUrlId desc) order by FriendUrlId) order by FriendUrlId desc";
            }
            using (var dr = DBHelper.ExecuteReader(strSql))
            {
                return FillDate(dr);
            }
        }

        public static int GetFriendUrlListCount()
        {
            return  DBHelper.ExecuteIntScalar(string.Format("SELECT count(*) FROM FriendUrlTB"));
        }

        public static List<FriendUrlEntity> GetFriendUrlList(int isHidden)
        {
            List<FriendUrlEntity> list = new List<FriendUrlEntity>();
            using (var dr = DBHelper.ExecuteReader(string.Format("SELECT * FROM FriendUrlTB where IsVisiable={0}", isHidden)))
            {
                return FillDate(dr);
            }
        }

        /// <summary>
        /// 根据条件获取信息列表
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public static List<FriendUrlEntity> GetFriendUrlListByCondition(string condition)
        {
            using (var dr = DBHelper.ExecuteReader(string.Format("SELECT * FROM FriendUrlTB WHERE {0}", condition)))
            {
                return FillDate(dr);
            }
        }

        /// <summary>
        /// 获取信息实体
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public static FriendUrlEntity GetFriendUrlEntity(string condition)
        {
            using (var dr = DBHelper.ExecuteReader(string.Format("SELECT * FROM FriendUrlTB WHERE {0}", condition)))
            {
                List<FriendUrlEntity> list = FillDate(dr);
                return list == null || list.Count == 0 ? (new FriendUrlEntity()) : list[0];
            }
        }

        /// <summary>
        /// 根据标识获取信息实体重载一
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public static FriendUrlEntity GetFriendUrlEntity(int friendUrlId)
        {
            return GetFriendUrlEntity(string.Format("SELECT * FROM FriendUrlTB WHERE FriendUrlId = {0}", friendUrlId));
        }

        /// <summary>
        /// 检查是否已经存在
        /// </summary>
        /// <param name="teamName"></param>
        /// <returns></returns>
        public static bool CheckFriendUrlIsExisted(string userName)
        {
            using (var dr = DBHelper.ExecuteReader(string.Format("SELECT * FROM FriendUrlTB WHERE FriendUrlName = '{0}'", userName)))
            {
                return (dr != null && dr.Read());
            }
        }

        /// <summary>
        /// 保存信息
        /// </summary>
        /// <param name="te"></param>
        /// <returns></returns>
        public static bool SaveFriendUrlInfor(FriendUrlEntity te)
        {
            int i = 0;
            if (te.FriendUrlId <= 0)
            {
                i = DBHelper.ExecuteNonQuery(string.Format("insert into FriendUrlTB (FriendUrlName,FriendUrl,IsVisiable,LogoPath,Remark) VALUES('{0}','{1}',{2},'{3}','{4}')", te.FriendUrlName, te.FriendUrl, te.IsVisiable, te.LogoPath, te.Remark));
            }
            else
            {
                i = DBHelper.ExecuteNonQuery(string.Format("UPDATE FriendUrlTB SET FriendUrlName ='{0}' ,FriendUrl ='{1}',IsVisiable={2},LogoPath='{3}',Remark='{4}' WHERE FriendUrlId = {5}", te.FriendUrlName, te.FriendUrl, te.IsVisiable, te.LogoPath, te.Remark, te.FriendUrlId));
            }
            return i > 0 ? true : false;
        }

        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool DeleteFriendUrlInfor(int friendUrlId)
        {
            return DBHelper.ExecuteNonQuery(string.Format("DELETE FROM FriendUrlTB WHERE FriendUrlID = {0}", friendUrlId)) >= 0 ? true : false;
        }
        #endregion

        #region 私有方法
        /// <summary>
        /// 填充数据
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        private static List<FriendUrlEntity> FillDate(IDataReader dr)
        {
            List<FriendUrlEntity> list = new List<FriendUrlEntity>();
            FriendUrlEntity te;
            while (dr != null && dr.Read())
            {
                te = new FriendUrlEntity();
                te.FriendUrlId = ConvertHelper.IntParse(dr["FriendUrlId"]);
                te.FriendUrlName = ConvertHelper.StringParse(dr["FriendUrlName"]);
                te.FriendUrl = ConvertHelper.StringParse(dr["FriendUrl"]);
                te.IsVisiable = ConvertHelper.IntParse(dr["IsVisiable"]);
                te.LogoPath = ConvertHelper.StringParse(dr["LogoPath"]);
                te.Remark = ConvertHelper.StringParse(dr["Remark"]);

                list.Add(te);
            }
            return list;
        }
        #endregion
    }
}
